<template>
  <div class="checkout-container">
    <h2>订单结算</h2>

    <div class="checkout-content">
      <div class="checkout-form">
        <div class="form-section">
          <h3>收货信息</h3>
          <div class="form-grid">
            <div class="form-group">
              <label>收货人姓名</label>
              <input type="text" v-model="shippingInfo.name" placeholder="请输入收货人姓名">
            </div>
            <div class="form-group">
              <label>联系电话</label>
              <input type="tel" v-model="shippingInfo.phone" placeholder="请输入联系电话">
            </div>
            <div class="form-group full-width">
              <label>详细地址</label>
              <input type="text" v-model="shippingInfo.address" placeholder="请输入详细地址">
            </div>
          </div>
        </div>

        <div class="form-section">
          <h3>支付方式</h3>
          <div class="payment-methods">
            <label class="payment-method">
              <input type="radio" v-model="paymentMethod" value="wechat">
              <span class="method-icon"><i class="fab fa-weixin"></i></span>
              <span class="method-name">微信支付</span>
            </label>
            <label class="payment-method">
              <input type="radio" v-model="paymentMethod" value="alipay">
              <span class="method-icon"><i class="fab fa-alipay"></i></span>
              <span class="method-name">支付宝</span>
            </label>
          </div>
        </div>

        <div class="form-section">
          <h3>订单备注</h3>
          <textarea 
            v-model="orderNote" 
            placeholder="请输入订单备注（选填）"
            rows="3"
          ></textarea>
        </div>
      </div>

      <div class="order-summary">
        <h3>订单摘要</h3>
        
        <div class="order-items">
          <div v-for="item in cartItems" :key="item.id" class="order-item">
            <img :src="item.image" :alt="item.name">
            <div class="item-details">
              <h4>{{ item.name }}</h4>
              <p class="item-quantity">数量: {{ item.quantity }}</p>
              <p class="item-price">¥{{ item.price * item.quantity }}</p>
            </div>
        </div>
        </div>

        <div class="summary-details">
          <div class="summary-row">
            <span>商品总数</span>
            <span>{{ totalItems }} 件</span>
          </div>
          <div class="summary-row">
            <span>商品总价</span>
            <span class="price">¥{{ totalPrice }}</span>
          </div>
          <div class="summary-row">
            <span>运费</span>
            <span>¥{{ shipping }}</span>
          </div>
          <div class="summary-row grand-total">
            <span>应付总额</span>
            <span class="grand-total-price">¥{{ grandTotal }}</span>
          </div>
        </div>

        <button 
          @click="submitOrder" 
          class="submit-order-btn"
          :disabled="!isFormValid"
        >
          提交订单
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

export default {
  name: 'Checkout',
  setup() {
    const store = useStore()
    const router = useRouter()

    const shippingInfo = ref({
      name: '',
      phone: '',
      address: ''
    })

    const paymentMethod = ref('wechat')
    const orderNote = ref('')

    const cartItems = computed(() => store.state.cart)
    const totalItems = computed(() => cartItems.value.reduce((sum, item) => sum + item.quantity, 0))
    const totalPrice = computed(() => cartItems.value.reduce((sum, item) => sum + item.price * item.quantity, 0))
    const shipping = computed(() => totalPrice.value > 1000 ? 0 : 50)
    const grandTotal = computed(() => totalPrice.value + shipping.value)
    
    const isFormValid = computed(() => {
      return shippingInfo.value.name && 
             shippingInfo.value.phone && 
             shippingInfo.value.address &&
             paymentMethod.value
    })

    const submitOrder = () => {
      if (!isFormValid.value) return

      // 创建订单对象
      const order = {
        items: cartItems.value,
        shippingInfo: shippingInfo.value,
        paymentMethod: paymentMethod.value,
        orderNote: orderNote.value,
        totalAmount: grandTotal.value,
        orderDate: new Date().toISOString(),
        orderStatus: 'paid'
      }

      // 保存订单到本地存储
      const orders = JSON.parse(localStorage.getItem('orders') || '[]')
      orders.push(order)
      localStorage.setItem('orders', JSON.stringify(orders))

      // 清空购物车
      store.commit('clearCart')

      // 显示成功提示并返回首页
      alert('订单提交成功！感谢您的购买。')
      router.push('/')
    }
    
    return {
      shippingInfo,
      paymentMethod,
      orderNote,
      cartItems,
      totalItems,
      totalPrice,
      shipping,
      grandTotal,
      isFormValid,
      submitOrder
    }
  }
}
</script>

<style scoped>
.checkout-container {
  padding: 40px;
  max-width: 1200px;
  margin: 0 auto;
  min-height: calc(100vh - 60px);
  background: var(--cf-white);
}

.checkout-container h2 {
  color: var(--cf-black);
  border-bottom: 2px solid var(--cf-orange);
  padding-bottom: 10px;
  margin-bottom: 30px;
  font-size: 1.8em;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.checkout-content {
  display: grid;
  grid-template-columns: 1.5fr 1fr;
  gap: 30px;
  animation: fadeIn 0.5s ease;
}

.form-section {
  background: var(--cf-light-gray);
  padding: 25px;
  border-radius: 12px;
  margin-bottom: 20px;
}

.form-section h3 {
  color: var(--cf-black);
  margin: 0 0 20px 0;
  font-size: 1.3em;
  border-bottom: 2px solid var(--cf-orange);
  padding-bottom: 10px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group.full-width {
  grid-column: 1 / -1;
}

.form-group label {
  color: var(--cf-gray);
  font-size: 0.9em;
}

.form-group input {
  padding: 12px;
  border: 1px solid var(--cf-gray);
  border-radius: 8px;
  font-size: 1em;
  transition: all 0.3s ease;
}

.form-group input:focus {
  border-color: var(--cf-blue);
  outline: none;
  box-shadow: 0 0 0 2px rgba(0, 120, 160, 0.1);
}

.payment-methods {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}

.payment-method {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 15px;
  border: 1px solid var(--cf-gray);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.payment-method:hover {
  border-color: var(--cf-blue);
}

.payment-method input[type="radio"] {
  display: none;
}

.payment-method input[type="radio"]:checked + .method-icon {
  color: var(--cf-blue);
}

.payment-method input[type="radio"]:checked + .method-icon + .method-name {
  color: var(--cf-blue);
  font-weight: 500;
}

.method-icon {
  font-size: 1.5em;
  color: var(--cf-gray);
}

.method-icon .fa-weixin {
  color: #07C160;
}

.method-icon .fa-alipay {
  color: #0078A0;
}

textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--cf-gray);
  border-radius: 8px;
  font-size: 1em;
  resize: vertical;
  transition: all 0.3s ease;
}

textarea:focus {
  border-color: var(--cf-blue);
  outline: none;
  box-shadow: 0 0 0 2px rgba(0, 120, 160, 0.1);
}

.order-summary {
  background: var(--cf-light-gray);
  padding: 25px;
  border-radius: 12px;
  position: sticky;
  top: 20px;
}

.order-summary h3 {
  color: var(--cf-black);
  margin: 0 0 20px 0;
  font-size: 1.3em;
  border-bottom: 2px solid var(--cf-orange);
  padding-bottom: 10px;
}

.order-items {
  max-height: 300px;
  overflow-y: auto;
  margin-bottom: 20px;
}

.order-item {
  display: flex;
  gap: 15px;
  padding: 15px 0;
  border-bottom: 1px solid var(--cf-gray);
}

.order-item:last-child {
  border-bottom: none;
}

.order-item img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 8px;
}

.item-details {
  flex: 1;
}

.item-details h4 {
  margin: 0 0 5px 0;
  color: var(--cf-black);
  font-size: 1em;
}

.item-quantity {
  color: var(--cf-gray);
  font-size: 0.9em;
  margin: 0 0 5px 0;
}

.item-price {
  color: var(--cf-orange);
  font-weight: bold;
  margin: 0;
}

.summary-details {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--cf-gray);
}

.summary-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  color: var(--cf-gray);
}

.summary-row.grand-total {
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid var(--cf-gray);
  color: var(--cf-black);
  font-weight: bold;
  font-size: 1.1em;
}

.price, .grand-total-price {
  color: var(--cf-orange);
  font-weight: bold;
}

.submit-order-btn {
  width: 100%;
  padding: 15px;
  background: var(--cf-blue);
  color: var(--cf-white);
  border: none;
  border-radius: 8px;
  font-size: 1.1em;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 20px;
}

.submit-order-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.submit-order-btn:not(:disabled):hover {
  background: var(--cf-dark-blue);
  transform: translateY(-2px);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .checkout-container {
    padding: 20px;
}

  .checkout-content {
    grid-template-columns: 1fr;
}

  .form-grid {
    grid-template-columns: 1fr;
}

  .payment-methods {
    grid-template-columns: 1fr;
  }

  .order-summary {
    position: static;
  }
}
</style>